package com.demo.cn.retrofirdemo;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import java.io.IOException;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Converter;
import retrofit2.Response;
import retrofit2.Retrofit;

public class MainActivity extends AppCompatActivity {
    TextView viewById;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         viewById = (TextView) findViewById(R.id.btn);
        initView();
    }

    private void initView() {
        Response<String> execute;
        viewById.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // 第2部分：在创建Retrofit实例时通过.baseUrl()设置
                Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("http://222.68.185.232:9080/EntityChannelInteface/") //设置网络请求的Url地址
                        .addConverterFactory(new ToStringConverterFactory())
                        .build();
                ApiServices apiServices = retrofit.create(ApiServices.class);
//                final Call<String> call = apiServices.getCall();
//               final Call<String> call = apiServices.doLogin("tangjiaqi", "zxcv");

                //json
                String route = "{\"content\":\"{\\\"storeId\\\":\\\"AA002\\\"}\"}";
                RequestBody body=RequestBody.create(MediaType.parse("application/json;charset=utf-8"),route);
                final Call<String> call = apiServices.getExpandDetail(body);

                //这里是异步方式的调用
//                call.execute()//这里是同步方式的调用 需要在异步线程中调用。常用下边这种调用方式。
                call.enqueue(new Callback<String>() {
                    @Override
                    public void onResponse(Call<String> call, Response<String> response) {
                        Log.e("ABC", "onResponse: "+response.body().toString() );
                        viewById.setText(response.body().toString()+"");
                    }

                    @Override
                    public void onFailure(Call<String> call, Throwable t) {

                    }
                });

            }
        });

    }

    public class ToStringConverterFactory extends Converter.Factory {
        private  final MediaType MEDIA_TYPE = MediaType.parse("text/plain");


        @Override
        public Converter<ResponseBody, ?> responseBodyConverter(Type type, Annotation[] annotations, Retrofit retrofit) {
            if (String.class.equals(type)) {
                return new Converter<ResponseBody, String>() {
                    @Override
                    public String convert(ResponseBody value) throws IOException {
                        return value.string();
                    }
                };
            }
            return null;
        }

        @Override public Converter<?, RequestBody> requestBodyConverter(Type type, Annotation[] parameterAnnotations,
                                                                        Annotation[] methodAnnotations, Retrofit retrofit) {
            if (String.class.equals(type)) {
                return new Converter<String, RequestBody>() {
                    @Override
                    public RequestBody convert(String value) throws IOException {
                        return RequestBody.create(MEDIA_TYPE, value);
                    }
                };
            }
            return null;
        }
    }

}
